
angular.module('U9ImPDA', [
    'ionic',
    'ngCordova',
    'oc.lazyLoad',
    'Ionic.ImPDA',

    'U9ImPDA.controllers',
    'U9ImPDA.directives',
    'U9ImPDA.services',
    'U9ImPDA.utility'
])

    .run(['$ionicPlatform', '$timeout', 'InitService', 'APPCONSTANTS',
        function ($ionicPlatform, $timeout, InitService, APPCONSTANTS) {
            $ionicPlatform.ready(function () {
                // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
                // for form inputs)
                if (window.cordova && window.cordova.plugins.Keyboard) {
                    cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
                }
                if (window.StatusBar) {
                    StatusBar.styleDefault();
                }
        
                InitService.finally(function () {
                    if (navigator.splashscreen) {
                        $timeout(function () {
                            navigator.splashscreen.hide();
                            if (window.StatusBar) {
                                StatusBar.show();
                            }
                        }, APPCONSTANTS.splashScreenExtraDelay);
                    }
                });
            });
            ionic.Platform.isFullScreen = true;
        }
    ])

    .config(['$stateProvider', '$urlRouterProvider', '$ionicConfigProvider', '$httpProvider', '$ocLazyLoadProvider',
        function ($stateProvider, $urlRouterProvider, $ionicConfigProvider, $httpProvider, $ocLazyLoadProvider) {
            
            $httpProvider.interceptors.push('HttpInterceptor');

            $ocLazyLoadProvider.config({
                debug: false,
                events: true
            });

            $stateProvider
                .state('home', {
                    url: '/home',
                    templateUrl: 'tpls/home.html',
                    controller: 'HomeCtrl',
                    resolve: {
                        'loading': ['$q', '$state', 'Connect', 'InitService', 'Auth', 'imanDialog',
                            function ($q, $state, Connect, InitService, Auth, imanDialog) {
                                var defer = $q.defer();

                                InitService.finally(function () {
                                    if (!Connect.get('address') || !Connect.get('port')) {
                                        $state.go('connectsetting');
                                        imanDialog.toastBottom('请配置连接设置');
                                        defer.resolve();
                                        return;
                                    }
                                    if (!Auth.isAuth()) {
                                        $state.go('login');
                                        imanDialog.toastBottom('请登录');
                                        defer.resolve();
                                        return;
                                    }
                                    defer.resolve();
                                });

                                return defer.promise;
                            }
                        ]
                    }
                })
                .state('login', {
                    url: '/login',
                    templateUrl: 'tpls/login.html',
                    controller: 'LoginCtrl'
                })
                .state('register', {
                    url: '/register',
                    templateUrl: 'tpls/register.html',
                    controller: 'RegisterCtrl'
                })
                .state('connectsetting', {
                    url: '/connectsetting',
                    templateUrl: 'tpls/connectsetting.html',
                    controller: 'ConnectSettingCtrl'
                });
            $urlRouterProvider.otherwise('/home');

            $ionicConfigProvider.platform.android.scrolling.jsScrolling(true);
            $ionicConfigProvider.platform.android.navBar.alignTitle('center');
            $ionicConfigProvider.platform.android.backButton.previousTitleText(false);
            $ionicConfigProvider.platform.android.backButton.icon('ion-ios-arrow-back');
            $ionicConfigProvider.platform.android.spinner.icon('ios');
            $ionicConfigProvider.platform.android.views.swipeBackEnabled(true);
            $ionicConfigProvider.platform.android.views.swipeBackHitWidth(45);
            $ionicConfigProvider.platform.android.tabs.style('standard');
            $ionicConfigProvider.platform.android.tabs.position('bottom');
            $ionicConfigProvider.platform.android.form.toggle('large');

            $ionicConfigProvider.platform.default.backButton.previousTitleText(false);
            $ionicConfigProvider.platform.default.backButton.text(false);
        }
    ]);

(function () {
    if (typeof cordova === 'undefined') {
        angular.element(document).ready(onReady);
    } else {
        document.addEventListener("deviceready", onReady, false);
    }

    function onReady () {
        angular.bootstrap(document, ['U9ImPDA']);
    }
})();